package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.UnitpriceManureDao;
import cn.topevery.garden.entity.po.UnitpriceManure;
import cn.topevery.garden.entity.query.UnitpriceManureQuery;
import cn.topevery.garden.entity.vo.UnitpriceManureVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2022-01-24
 */
@Repository
public class UnitpriceManureDaoImpl extends BaseDaoImpl<UnitpriceManure> implements UnitpriceManureDao {
    @Override
    public <P extends DbCsp> List<UnitpriceManure> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<UnitpriceManureVO> listPage(UnitpriceManureQuery query) {
        String table = "t_unitprice_manure as price\n" +
                "        inner join t_sys_map as map on map.c_id = price.c_map_id\n" +
                "        inner join t_sys_user as usr on usr.c_id = price.c_creator_id";

        String columns = " price.c_id as id,\n" +
                "        price.c_entity_id as entityId,\n" +
                "        price.c_map_id as mapId,\n" +
                "        map.c_name as area,\n" +
                "        price.c_name as name,\n" +
                "        price.c_unitprice as unitprice,\n" +
                "        price.c_remark as remark,\n" +
                "        usr.c_user_name as creator,\n" +
                "        DATE_FORMAT(price.c_db_create_date, '%Y-%m-%d %H:%i:%s') as dbCreateDate";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("price.c_db_create_date desc");
        sqlQueryMate
                .where(true, "price.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "price.c_in_use", ScriptConditionEnum.Eq, 1)
                .where(!StringUtils.isEmpty(query.getName()), "price.c_name", ScriptConditionEnum.Like, query.getName())
                .where(!StringUtils.isEmpty(query.getMapId()), "price.c_map_id", ScriptConditionEnum.Eq, query.getMapId())

        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, UnitpriceManureVO.class);
    }

    public DataPager<UnitpriceManureVO> getHisList(UnitpriceManureQuery query) {
        String table = "t_unitprice_manure as price\n" +
                "        inner join t_sys_map as map on map.c_id = price.c_map_id\n" +
                "        inner join t_sys_user as usr on usr.c_id = price.c_creator_id";

        String columns = " price.c_id as id,\n" +
                "               map.c_name as area,\n" +
                "               price.c_name as manure_name,\n" +
                "               price.c_unitprice as unitprice,\n" +
                "               price.c_remark as remark,\n" +
                "               usr.c_user_name as creator,\n" +
                "        DATE_FORMAT(price.c_db_create_date, '%Y-%m-%d %H:%i:%s') as c_db_create_date";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("price.c_db_create_date desc");
        sqlQueryMate
                .where(true, "price.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(!StringUtils.isEmpty(query.getName()), "price.c_entity_id", ScriptConditionEnum.Like, query.getEntityId())
                .where(!StringUtils.isEmpty(query.getMapId()), "price.c_map_id", ScriptConditionEnum.Eq, query.getMapId())

        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, UnitpriceManureVO.class);
    }

    @Override
    public UnitpriceManure getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }
}
